/*
 * @Author: caofujun
 * @Date: 2020-09-04 14:48:46
 * @Description: 过滤器
 */
import { MEMBER_TXT, BAN_TXT, CASH_NAME } from './business'
import { getStr } from './index'
import moment from 'moment'

/**
 * 编号累加
 * @param {*} index 索引
 * @param {*} pageIndex 当前页码
 * @param {*} pageSize 页数量
 */
function indexMethod(index, pageIndex, pageSize = 10) {
  if (pageIndex === undefined) {
    throw Error('pageIndex 不能为 undefined')
  }
  return (pageIndex - 1) * pageSize + index + 1
}

/**
 * 保留小数位，
 * @param {*} val
 * @param {*} acc 保留几位小数位
 */
function toFixed(val, acc) {
  let num = parseFloat(val)
  if (isNaN(num)) {
    num = 0
  }
  let accuracy = parseInt(acc)
  if (isNaN(accuracy) || accuracy < 0 || accuracy > 10) {
    accuracy = 2
  }
  return num.toFixed(accuracy)
}

/**
 * 金额格式化
 * @param {Number} value 金额
 * @param {Number} num 保留小数位
 * @param {Boolean} isLeftStr 左侧是否有￥字符
 */
export function formatMoney(value, num = 2, isLeftStr = false) {
  if (isNaN(Number(value))) {
    return isLeftStr ? '¥ 0.00' : '0.00'
  }
  num = num > 0 && num <= 20 ? num : 2
  value = parseFloat((value + '').replace(/[^\d\.-]/g, '')).toFixed(num) + ''
  const valueArr = value.split('.')[0].split('').reverse()
  const valueFloat = value.split('.')[1]
  let valueString = ''
  for (let i = 0; i < valueArr.length; i++) {
    valueString += valueArr[i] + ((i + 1) % 3 === 0 && (i + 1) !== valueArr.length ? ',' : '')
  }
  const money = valueString.split('').reverse().join('') + '.' + valueFloat
  return isLeftStr ? ' ¥ ' + money : money
}

/**
 * 获取数组对象某一值value
 * @param {*} arr  数组
 * @param {*} baseKey
 * @param {*} getKey
 */
export function getArrValue(baseValue, baseKey, getKey, arr) {
  const item = arr.find((item) => {
    return item[baseKey] === baseValue
  })
  return item && item[getKey] ? item[getKey] : ''
}

export function getNewArrayValue(array, key = 'value') {
  const res = []
  if (array) {
    array.forEach((t) => {
      if (t[key]) res.push(t[key])
    })
  }
  return res
}

/**
 * 数据脱敏
 * @param {*} str 转换的字符串
 * @param {*} beginLen 从字符串下标的第几位开始
 * @param {*} endLen 从字符串下标的第几位结束
 * @returns {string|*} 返回转换好的字符串
 */
function desensitization(str, beginLen = 1, endLen = -4) {
  if (!str) {
    return ''
  }
  const len = str.length
  if (len === 2) {
    return str.substring(0, 1) + '*'
  } else if (len === 3) {
    return str.substring(0, 1) + '*' + str.substring(2, 3)
  } else if (len > 3) {
    const firstStr = str.substr(0, beginLen)
    const lastStr = str.substr(endLen)
    const middleStr = str.substring(beginLen, len - Math.abs(endLen)).replace(/[\s\S]/ig, '*')
    const tempStr = firstStr + middleStr + lastStr
    return tempStr
  }
}

const intFilter = (value) => {
  if (!value) return ''
  return value.replace(/\D/g, '')
}

export function inputFilter(value, type = 'init') {
  let temp = null
  switch (type) {
    case 'init':
      temp = intFilter(value)
      break

    default:
      temp = ''
      break
  }
  return temp
}

/**
 * 获取会员等级
 */
export const getMenmberGradeName = (type) => {
  return MEMBER_TXT[type] ? MEMBER_TXT[type] : ''
}

export const getBanTxt = (type) => {
  return BAN_TXT[type] ? BAN_TXT[type] : ''
}

// --- 提现相关 ----

const getCashName = (type) => {
  return CASH_NAME[type] ? CASH_NAME[type] : ''
}
/**
 *
 * @param {*} region 地区
 * @param {*} address 详细地址
 */
function formatAddress(region, address) {
  try {
    const temp = JSON.parse(region).join('/')
    return `${temp}  ${address}`
  } catch (error) {
    return `${region}  ${address}`
  }
}

/**
 *
 * @param {*} timestamp
 * @param {*} format 日期的格式 yyyy MM dd
 */
export function formatDate(timestamp, format = 'YYYY-MM-DD') {
  if (timestamp) {
    return moment(timestamp).format(format)
  }
  return '未知'
}

function getAccountType(key) {
  // 供应商账户类型
  const ACCOUNTTYPE = {
    1: '对公账户'
  }
  return ACCOUNTTYPE[key] ? ACCOUNTTYPE[key] : '未知'
}
export default { getAccountType, formatDate, formatAddress, indexMethod, toFixed, formatMoney, getArrValue, desensitization, inputFilter, getMenmberGradeName, getBanTxt, getCashName, getStr }
